package com.zg.scf.service.impl;


import com.zg.common.core.dao.scf.ProductTypeDao;
import com.zg.common.core.dao.scf.entity.Credit;
import com.zg.common.core.dao.scf.entity.ProductType;
import com.zg.common.core.service.PageBaseService;
import com.zg.scf.domain.req.StateReq;
import com.zg.scf.service.CreditService;
import com.zg.scf.service.ProductTypeService;
import com.zg.scf.enm.StateEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.zg.scf.constant.ScfE.*;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/5/22 10:47
 */



@Service
@Slf4j
public class ProductTypeServiceImpl extends PageBaseService<ProductType, ProductTypeDao> implements ProductTypeService {

    @Resource
    private CreditService creditService;
    protected  void preRemove(List<Long> ids) {
        for(int i=0;i<ids.size();i++){
            ProductType productType = baseDao.getById(ids.get(i));
            if(productType.getState().equals(StateEnum.Y.getValue())){
                PRODUCT_TYPE_IS_NORMAL.happen();
            }
            List<Credit> credits =creditService.listByProductType(productType.getId());
            if(!credits.isEmpty()){
                PRODUCT_TYPE_IS_USED.happen();
            }
        }
    }

    @Override
    protected void preSource(ProductType source) {
        ProductType productType = baseDao.getById(source.getId());
        if(productType.getState().equals(StateEnum.Y.getValue())){
            PRODUCT_TYPE_IS_NORMAL.happen();
        }
    }

    public void editState(StateReq req) {
        ProductType productType = baseDao.getById(req.getId());
        productType.setState(req.getState());
        baseDao.updateById(productType);
    }
    
}
